// pages/clockStatis/clockStatis.js
const app = getApp()
const util = require('../../../../utils/util.js')
const lbbService = require('../../../service/service.js')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    pageNum:1,
    pageSize:20,
    members: [],
    index: 0,
    month: '',
    day: '01',
    dayIndex: 0,
    hospitalId: '',
    hospitalName: '全部',
    typeIndex: 0,
    memberIndex: 0,
    memberArray: [], //出差人员
    members: [],
    typeArray: [], //出差类型
    types: [], //出差类型
    data: [{
      "id": 31,
      "types": ["安装新控制盒","安装新控制盒"],
      "memo":'备注'
    }]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this
    let date = that.getNowDate();
    that.setData({
      nowDate: date,
      startTime: date,
      endTime: date,
      riginDate: '2020-01-01',
      scrollHeight:app.getScrollHeight(210)
    })
    that.getSignTypes();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let that = this;

    if (that.data.changedLocationInfo) {
      let hospitalId = that.data.changedLocationInfo.hospitalId;
      let hospitalName = that.data.changedLocationInfo.hospitalName;
      that.setData({
        hospitalName: hospitalName,
        hospitalId: hospitalId
      })
      that.data.pageNum = 1;
      that.getRecords();
    }

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 查看巡检报告
   */
  toReportDetail(e) {
    console.log(e,'打印E')
    let id=e.currentTarget.dataset.id;
    if(e.currentTarget.dataset.lbmid){
      var hospital = e.currentTarget.dataset.lbmid;
    }
    wx.navigateTo({
      url: '/pages/patrolReport/patrolReport?reportId='+id+'&hospitalId='+hospital,
    })
  },
  /**
   * 获取出差类型
   */
  getSignTypes: function () {
    let that = this;
    lbbService.getSignTypes({}, function (res) {
      let resData = res.data;
      resData.unshift('全部');
      that.setData({
        typeArray: resData,
        types: resData
      })
      that.getMembers();
    })
  },


  /**
   * 获取出差人员
   */
  getMembers: function () {
    let that = this
    lbbService.getMembers({}, function (res) {
      let members = res.data
      let item = {}
      item.masterName = '全部'
      item.masterUid = ''
      members.unshift(item);
      that.setData({
        members: members
      })
      that.setName();
    })
  },
  setName: function () {
    let that = this
    let members = that.data.members
    let array = []
    members.forEach(item => {
      array.push(item.masterName)
    })
    that.setData({
      memberArray: array
    })
    that.data.pageNum = 1;
    that.getRecords();
  },

  /**
   * 选择出差类型
   */
  typeChange(e) {
    let that = this;
    let typeIndex = e.detail.value;
    that.setData({
      typeIndex: typeIndex
    })
    that.data.pageNum = 1;
    that.getRecords();
  },
  /**
   * 选择出差人员
   */
  memberChange(e) {
    let that = this;
    let memberIndex = e.detail.value;
    that.setData({
      memberIndex: memberIndex
    })
    that.data.pageNum = 1;
    that.getRecords();
  },
  /**
   * 选择医院
   */
  toChooseHospital: function () {
    wx.navigateTo({
      url: '../../../chooseHospital/chooseHospital',
    })
  },

  /**
   * 选择时间
   */
  startTimeChange: function (e) {
    let that = this;
    that.setData({
      startTime: e.detail.value
    })
    that.data.pageNum = 1;
    that.getRecords();
  },

  /**
   * 选择时间
   */
  endTimeChange: function (e) {
    let that = this;
    that.setData({
      endTime: e.detail.value
    })
    that.data.pageNum = 1;
    that.getRecords();
  },


  /**
   * 查看巡检记录
   */
  toDetail: function (e) {
    console.log('toDetail>>', e)
    let cardDate = e.currentTarget.dataset.date
    let uid = e.currentTarget.dataset.uid
    let type = e.currentTarget.dataset.type==1?1:2
    wx.navigateTo({
      url: '../clockDetail/clockDetail?cardDate=' + cardDate + '&uid=' + uid + '&type='+type,
    })
  },

  /**
   * 获取出差数据
   */
  getRecords: function () {
    let that = this;
    lbbService.getTripList({
      cardStartTime: that.data.startTime,
      cardEndTime: that.data.endTime,
      pageNum:that.data.pageNum,
      pageSize:that.data.pageSize,
      uid: that.data.members[that.data.memberIndex].masterUid,
      businessTravelReasonType: that.data.types[that.data.typeIndex] == '全部' ? '' : that.data.types[that.data.typeIndex],
      hospitalId: that.data.hospitalId,
    }, function (res) {
      that.setData({
        tripList: that.data.pageNum==1?res.data:that.data.tripList.concat(res.data)
      })
    })
  },
  /**
   * 翻页
   */
  getMore: function() {
    this.data.pageNum++;
    this.getRecords();
  },

  getNowDate: function () {
    let reg = /\//g
    let nowTime = new Date()
    let nowDate = util.formatTime(nowTime).substr(0, 10).replace(reg, '-')
    return nowDate
  },

  getDays: function (date) {
    var d = new Date(parseInt(date.substr(0, 4)), parseInt(date.substr(5, 6)), 0)
    let num = d.getDate()
    let days = ['整月']
    for (let i = 0; i < num; i++) {
      days.push(i + 1 + '号')
    }
    return days
  }
})